/*
 * Copyright (c) Hunan University.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <iostream>
#include "nlcpp.h"

using namespace nlcpp;
using namespace std;

void test_interpolation() {
    Vector<double> xs(3);
    Vector<double> ys(3);
    xs << 100, 121, 144;
    ys << 10, 11, 12;

    
    Vector<double> x(1);
    x << 115;

    cout << "========Test Interpolation (Case 1) ...========" << endl;

    Vector<double> y_lagrange = Interpolation::interpolate("lagrange", xs, ys, x);
    cout << "Result by lagrange interpolation " << endl << y_lagrange << endl;

    Vector<double> y_newton = Interpolation::interpolate("newton", xs, ys, x);
    cout << "Result by newton interpolation " << endl << y_newton << endl;

    Vector<double> y_piecewise_lagrange = Interpolation::piecewise_interpolate("lagrange", xs, ys, 2, x);
    cout << "Result by piecewise lagrange interpolation " << endl << y_piecewise_lagrange << endl;

    Vector<double> y_piecewise_newton = Interpolation::piecewise_interpolate("newton", xs, ys, 2, x);
    cout << "Result by piecewise newton interpolation " << endl << y_piecewise_newton << endl;

    Vector<double> xs2(5);
    Vector<double> ys2(5);
    xs2 << -2, -1, 0, 1, 3;
    ys2 << -56, -16, -2, -2, 4;


    Vector<double> x2(1);
    x2 << 4;

    cout << "========Test Interpolation (Case 2) ...========" << endl;

    y_lagrange = Interpolation::interpolate("lagrange", xs2, ys2, x2);
    cout << "Result by lagrange interpolation " << endl << y_lagrange << endl;

    y_newton = Interpolation::interpolate("newton", xs2, ys2, x2);
    cout << "Result by newton interpolation " << endl << y_newton << endl;

    y_piecewise_lagrange = Interpolation::piecewise_interpolate("lagrange", xs2, ys2, 4, x2);
    cout << "Result by piecewise lagrange interpolation " << endl << y_piecewise_lagrange << endl;

    y_piecewise_newton = Interpolation::piecewise_interpolate("newton", xs2, ys2, 4, x2);
    cout << "Result by piecewise newton interpolation " << endl << y_piecewise_newton << endl;

    cout << endl;
}
